package thread;

import java.util.Scanner;

/**
 * notifyAll() 唤醒所有线程是随机的
 */
public class demo21 {
    public static void main(String[] args) {
        Object lock = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("t1 wait 之前");
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t1 wait 之后");
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("t2 wait 之前");
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t2 wait 之后");
            }
        });

        Thread t3 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("t3 wait 之前");
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t3 wait 之后");
            }
        });

        Thread t4 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("t4 wait 之前");
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t4 wait 之后");
            }
        });

        t1.start();
        t2.start();
        t3.start();
        t4.start();

        Thread t5 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("t5 notify 之前");
                Scanner scanner = new Scanner(System.in);
                System.out.println("输入任意字符唤醒: ");
                scanner.next();
                lock.notifyAll();
                System.out.println("t5 notify 之后");
            }
        });

        t5.start();

    }

}
